package 微软笔试.task2;

import java.util.Arrays;

public class Solution {

    public static void main(String[] args) {
        int[] A = new int[]{3,5,1,3,9,8};
        int K = 4;
        System.out.println(solution(A,K));
    }
    public static int solution(int[] A, int K) {
        int n = A.length;
        int res = Integer.MAX_VALUE;
        for (int i = 0; i < n; i++) {
            int min = Integer.MAX_VALUE;
            int max = 0;
            for(int j = 0;j<i;j++){
                min = Math.min(min,A[j]);
                max = Math.max(max,A[j]);
            }
            for(int j = i+K;j<n;j++){
                min = Math.min(min,A[j]);
                max = Math.max(max,A[j]);
            }
            res = Math.min(res,max-min);
        }
        return res;
    }
}
